package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.kaishengit.entity.AtMessage;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class AtMessageDao {

	public DBHelp<AtMessage> db = new DBHelp<AtMessage>();
	
	public void save(AtMessage atMessage){
		String sql = "insert into t_at_message(id,userId,messageId,projectId) values (?,?,?,?)";
		db.executeSQL(sql, atMessage.getId(),atMessage.getUserId(),atMessage.getMessageId(),atMessage.getProjectId());
	}
	
	public void update(AtMessage atMessage){
		String sql = "update t_at_message set userId = ?, messageId = ?,projectId = ? where id = ?";
		db.executeSQL(sql, atMessage.getUserId(),atMessage.getMessageId(),atMessage.getProjectId(),atMessage.getId());
	}
	public void del(String id) {
		String sql = "delete from t_at_message where id = ?";
		db.executeSQL(sql, id);
	}
	
	public AtMessage findById(int id) {
		String sql = "select id,userId,messageId,projectId from t_at_message where id = ?";
		return db.executeQueryToObject(sql, new AtMessageRowMapper(), id);
	}
	
	private class AtMessageRowMapper implements RowMapper<AtMessage> {

		public AtMessage mapRow(ResultSet rs) throws SQLException {
			AtMessage atMessage = new AtMessage();
			atMessage.setId(rs.getInt("id"));
			atMessage.setUserId(rs.getInt("userId"));
			atMessage.setMessageId(rs.getInt("messageId"));
			atMessage.setProjectId(rs.getInt("projectId"));
			return atMessage;
		}
		
	}
	
}
